package com.controller.user;

import com.entity.GoodCollectionItem;
import com.service.user.CollectionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/** 和收藏相关的控制器 */
@RestController
@CrossOrigin
public class CollectionController {

  @Autowired CollectionService collectionService;

  /**
   * 获取收藏条目数量
   *
   * @param userId 用户编号
   * @return 收藏条目数量
   */
  @GetMapping("collection/getGoodCollectionCount")
  public String getGoodCollectionCount(@RequestParam("userId") String userId) {
    int count = collectionService.getGoodCollectionCount(userId);
    return String.valueOf(count);
  }
  /**
   * 获取用户编号为userId的用户的第pageIndex页的收藏商品条目列表
   *
   * @param userId 用户编号
   * @param pageSize 页大小
   * @param pageIndex 第几页(从0开始)
   * @return 对应页的商品收藏列表
   */
  @GetMapping("collection/getCollectionGoodsList")
  public List<GoodCollectionItem> getCollectionGoodsList(
      @RequestParam("userId") String userId,
      @RequestParam("pageSize") int pageSize,
      @RequestParam("pageIndex") int pageIndex) {
    List<GoodCollectionItem> cl =
        collectionService.getGoodsCollectList(userId, pageSize, pageIndex);
    return cl;
  }

  /**
   * 删除用户编号为userId的用户的编号为goodId的商品收藏条目
   *
   * @param userId 用户编号
   * @param goodId 待删除条目的商品编号
   * @return 删除成功返回1 否则返回0
   */
  @GetMapping("collection/delCollectionItem")
  public String delCollectionItem(
      @RequestParam("userId") String userId, @RequestParam("goodId") String goodId) {
    int ret = collectionService.delGoodsCollectionItem(userId, goodId);
    return String.valueOf(ret);
  }
}
